﻿using Dropbox.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace dev.queensland.web.Controllers
{
    public class DropboxController : Controller
    {
        static async Task ListRootFolder(DropboxClient dbx)
        {
            var list = await dbx.Files.ListFolderAsync(string.Empty);

            // show folders then files
            foreach (var item in list.Entries.Where(i => i.IsFolder))
            {
                Console.WriteLine("D  {0}/", item.Name);
            }

            foreach (var item in list.Entries.Where(i => i.IsFile))
            {
                Console.WriteLine("F{0,8} {1}", item.AsFile.Size, item.Name);
            }
        }

        public async Task Upload(DropboxClient dbx, string folder, string file, string content)
        {
            using (var mem = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
            {
                var updated = await dbx.Files.UploadAsync(
                    folder + "/" + file,
                    Dropbox.Api.Files.WriteMode.Overwrite.Instance,
                    body: mem);
                Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
            }
        }

        public async Task<string> Run()
        {
            using (var dbx = new DropboxClient("9_bgJRHaxuAAAAAAAAAAg0gZurnOM8y_d3AzeTwwYjM16ipsfSVqQ4gqblIQlOgV"))
            {
                //var full = await dbx.Users.GetCurrentAccountAsync();

                //dbx.Auth.TokenRevokeAsync();

                //// Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);
                //string s = string.Format("{0} - {1}", full.Name.DisplayName, full.Email);
                //return s;

                return "asd";
            }

        }

        //async Task Upload(DropboxClient dbx, string folder, string file, string content)
        //{
        //    var dbx = new DropboxClient("9_bgJRHaxuAAAAAAAAAAg0gZurnOM8y_d3AzeTwwYjM16ipsfSVqQ4gqblIQlOgV");

        //    using (var mem = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
        //    {
        //        var updated = await dbx.Files.UploadAsync(
        //            folder + "/" + file,
        //            Dropbox.Api.Files.WriteMode.Overwrite.Instance,
        //            body: mem);
        //        Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
        //    }
        //}

        // GET: Dropbox
        public ActionResult Index()
        {
            var dbx = new DropboxClient("9_bgJRHaxuAAAAAAAAAAg0gZurnOM8y_d3AzeTwwYjM16ipsfSVqQ4gqblIQlOgV");
            Dropbox.Api.Files.ListFolderResult result = dbx.Files.ListFolderAsync(string.Empty).Result;

            return View();
        }

        // GET: Dropbox/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Dropbox/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Dropbox/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Dropbox/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Dropbox/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Dropbox/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Dropbox/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
